<template>
  <section>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="real_name"
          placeholder="用户"
          clearable
          class="handle-input mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="mobile"
          placeholder="手机号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="card_number"
          placeholder="卡号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-select
          v-model="searchChannelId"
          placeholder="筛选通道"
          clearable
          @keyup.enter.native="handleSearch"
          class="mr10"
        >
          <!-- <el-option label="全部" value=""></el-option> -->
          <el-option v-for="(item, index) in channel" :value="item.id" :key="index" :label="item.show_name"></el-option>
        </el-select>
        <el-select
          v-model="status"
          placeholder="筛选状态"
          clearable
          @keyup.enter.native="handleSearch"
          class="mr10"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="已制定" value="1"></el-option>
          <el-option label="进行中" value="2"></el-option>
          <el-option label="已完成" value="3"></el-option>
          <el-option label="已取消" value="4"></el-option>
          <el-option label="已失败" value="5"></el-option>
        </el-select>
        
        <el-button type="primary" icon="search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="danger" @click="handleReset">清空</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        v-loading="loading"
      >
        <!-- <el-table-column
          type="selection"
          width="40"
          align="center"> -->
        <!-- </el-table-column> -->
        <el-table-column prop="id" label="计划ID" width="80"></el-table-column>
        <el-table-column
          prop="real_name"
          label="姓名"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="电话"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="amount"
          label="计划金额"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="card_number"
          label="银行卡号"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="bank_name"
          label="银行名称"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="channel_name"
          label="通道"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="update_time"
          label="更新时间"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="unsettled_amount"
          label="未结算金额"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          :formatter="statusFormat"
          width="150"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              class="mr10"
              @click="showInfo(scope.row)"
              >详情</el-button
            ><el-button
            v-if="
              (scope.row.plan_status != 1 && scope.row.plan_status != 2)
            "
              type="primary"
              size="small"
              class="mr10"
              @click="redevelopment(scope.row)"
              >重新制定</el-button
            >
            <el-button
              v-if="
                (scope.row.status == 4 || scope.row.status == 5) &&
                  parseFloat(scope.row.unsettled_amount) > 0.0
              "
              type="danger"
              size="small"
              class="mr10"
              @click="handleRefund(scope.row)"
              >退款</el-button
            >
            <el-popover
              v-if="scope.row.status == 2 || scope.row.status == 1"
              placement="bottom"
              title="确定要取消该计划吗？"
              width="200"
              trigger="click"
            >
              <el-row>
                <el-button type="primary">点错了</el-button>
                <el-button type="danger" @click="handleCancelPlan(scope.row.id)"
                  >是的，取消</el-button
                >
              </el-row>
              <el-button size="small" type="warning" slot="reference"
                >取消</el-button
              >
            </el-popover>
              <el-button  v-if="(scope.row.status == 5)" size="small" type="primary" slot="reference" @click="handleSmsVisible(scope.row.mobile)"
                  >短信提醒</el-button
                >
                <el-button  v-if="(scope.row.status == 5)" size="small" type="primary" slot="reference" @click="handleWechatVisible(scope.row.mobile)"
                  >微信提醒</el-button
                >
            </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align:center">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          layout="sizes, prev, pager, next,total"
          :total="pagination.total"
          :current-page.sync="pagination.currentPage"
          :page-size="pagination.pageSize"
          :page-sizes="pagination.pageSizes"
          :hide-on-single-page="true"
        >
        </el-pagination>
      </div>
      <div class="dialog">
        <el-dialog
          :title="infoTitle"
          :visible.sync="dialogVisible"
          :before-close="handleClose"
          v-loading.lock="infoLoading"
        >
          <el-table :data="gridData" max-height="800">
            <el-table-column
              property="id"
              label="详情ID"
              width="80"
            ></el-table-column>
            <el-table-column
              property="request_no"
              label="交易号"
            ></el-table-column>
            <el-table-column
              property="amount"
              label="金额"
              width="80"
            ></el-table-column>
            <el-table-column
              property="type"
              label="类型"
              :formatter="infoTypeFormat"
              width="80"
            ></el-table-column>
            <el-table-column
              property="run_time"
              label="执行时间"
              width="150"
            ></el-table-column>
            <el-table-column
              property="charge"
              label="手续费"
              width="80"
            ></el-table-column>
            <el-table-column
              property="status"
              label="状态"
              :formatter="infoStatusFormat"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-view"
                  v-if="scope.row.status == 1"
                  @click="handleStatusQuery(scope.row)"
                  >查询状态</el-button
                >
                <el-button
                  type="primary"
                  icon="el-icon-view"
                  v-if="scope.row.status == 3"
                  @click="handleManualExePlan(scope.row.id)"
                  >重新执行</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </div>
    <el-dialog title="发送短信" :visible.sync="smsVisible">
      <el-form :model="smsContent">
        <el-form-item label="发送手机号：" :label-width="formLabelWidth">
            {{this.smsContent.mobile}}
        </el-form-item>
        <el-form-item label="请输入发送短信内容" :label-width="formLabelWidth">
          <el-input v-model="smsContent.content" placeholder="请输入发送短信内容" style="width: 600px"></el-input>
        </el-form-item>
          <el-input v-model="smsContent.mobile" style="display:none"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="smsVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSmsSend">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="发送微信" :visible.sync="wechatVisible">
      <el-form :model="wechatContent">
        <el-form-item label="发送手机号：" :label-width="formLabelWidth">
            {{this.wechatContent.mobile}}
        </el-form-item>
        <el-form-item label="请输入发送微信内容" :label-width="formLabelWidth">
          <el-input v-model="wechatContent.content" placeholder="请输入发送微信内容" style="width: 600px"></el-input>
        </el-form-item>
          <el-input v-model="wechatContent.mobile" style="display:none"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="wechatVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleWechat">确 定</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {
  list,
  info,
  manualExePlan,
  cancelPlan,
  refund,
  redevelopment,
  statusQuery,
  smsSend,
  messageToWechat
} from '@/api/modules/plan'
import moment from 'moment'

export default {
  name: 'plan',
  components: {},
  data() {
    return {
      loading: true,
      infoLoading: false,
      infoTitle: '',
      dialogVisible: false,
      tableData: [],
      real_name: '',
      mobile: '',
      card_number: '',
      status: '',
      pagination: {
        total: 0,
        pageSize: 20,
        currentPage: 1,
        pageSizes: [10, 20, 30]
      },
      gridData: [],
      channel: [],
      searchChannelId: '',
      smsVisible: false,
      wechatVisible: false,
      formLabelWidth: '200px',
      smsContent: {},
      wechatContent: {}
    }
  },
  computed: {
    startTime: function() {
      console.log(this.timeRange)
      return this.timeRange ? this.timeRange[0] : null
    },
    endTime: function() {
      return this.timeRange ? this.timeRange[1] : null
    }
  },
  created() {
    this.getData()
  },
  methods: {
    search() {
      this.getData()
    },
    // 分页导航
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.getData()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    // 获取数据
    getData() {
      this.loading = true
      list({
        page: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        mobile: this.mobile,
        real_name: this.real_name,
        card_number: this.card_number,
        status: this.status,
        channel: this.searchChannelId
      }).then(res => {
        if (res.status === 'success') {
          this.tableData        = res.data.list
          this.channel          = res.data.channel
          this.pagination.total = res.data.pagination.total
        }
        this.loading = false
      })
    },
    dateFormatter(row, column) {
      return moment(row.create_time * 1000).format('YYYY-MM-DD')
    },
    statusFormat(row, column) {
      if (row.status === 0) {
        return <el-tag>未执行</el-tag>
      } else if (row.status === 1) {
        return <el-tag type="warning">已制定</el-tag>
      } else if (row.status === 2) {
        return <el-tag type="success">进行中</el-tag>
      } else if (row.status === 3) {
        return <el-tag type="primary">已完成</el-tag>
      } else if (row.status === 4) {
        return <el-tag type="info">已取消</el-tag>
      } else if (row.status === 5) {
        return <span style="color:#f00">{row.status_message}</span>
      }
    },

    infoStatusFormat(row, column) {
      if (row.status === 0) {
        return <el-tag>待执行</el-tag>
      } else if (row.status === 1) {
        return <el-tag type="warning">执行中</el-tag>
      } else if (row.status === 2) {
        return <el-tag type="success">成功</el-tag>
      } else if (row.status === 3) {
        return <span style="color:#f00">{row.fail_message}</span>
      }
    },

    infoTypeFormat(row, column) {
      if (row.type === 1) {
        return <span style="color:#F56C6C">消费</span>
      } else if (row.type === 2) {
        return <span style="color:#67C23A">还款</span>
      }
    },

    /**
     * 重置
     */
    handleReset() {
      this.page = 1
      this.pageSize = 20
      this.mobile = ''
      this.real_name = ''
      this.card_number = ''
      this.status = ''
      this.getData()
    },

    /**
     * 搜索
     */
    handleSearch() {
      // this.$router.replace({ query: '' })
      this.getData()
    },

    /**
     * handleClose
     */
    handleClose() {
      this.dialogVisible = false
    },

    /**
     * 取消计划
     */
    handleCancelPlan(id) {
      this.loading = true
      cancelPlan({
        plan_id: id
      }).then(res => {
        if (res.status === 'success') {
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].id === id) {
              this.tableData[i].status = 4
            }
          }
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        }
        this.loading = false
      })
    },

    /**
     * 获取计划详情
     */
    showInfo(row) {
      this.loading = true
      info({
        id: row.id
      }).then(res => {
        if (res.status === 'success') {
          this.gridData = res.data.detail
          ;(this.dialogVisible = true),
            (this.infoTitle =
              '用户' +
              row.real_name +
              '(' +
              row.mobile +
              ')卡片' +
              row.card_number +
              '在' +
              row.channel_name +
              '的计划详情')
        }
        this.loading = false
      })
    },

    /** 
      重新制定
    */
    redevelopment(row) {
      this.loading = true
      redevelopment({
        plan_id: row.id,
      }).then(res => {
        if (res.status === 'success') {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        }
        this.loading = false
      })
    },
    /**
     * 退款
     */
    handleRefund(row) {
      this.loading = true
      refund({
        plan_id: row.id,
        amount: row.unsettled_amount
      }).then(res => {
        if (res.status === 'success') {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        }
        this.loading = false
      })
    },

    /**
     * 手动执行计划
     */
    handleManualExePlan(id) {
      this.infoLoading = true
      manualExePlan({
        plan_detail_id: id
      }).then(res => {
        if (res.status === 'success') {
          for (let i = 0; i < this.gridData.length; i++) {
            if (this.gridData[i].id === id) {
              this.gridData[i].status = 1
            }
          }
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        }
        this.infoLoading = false
      })
    },

    /**
     * 查询状态
     */
    handleStatusQuery(row) {
      this.infoLoading = true
      statusQuery({
        order_code: row.request_no
      }).then(res => {
        this.infoLoading = false
        if (res.status === 'success') {
          this.showInfo(row.plan_id)
        }
      })
    },

    /**
     * 表格选择
     */
    handleSelectionChange(value){
      console.log(value)
    },

    /**
     * 打开发送短信对话框
     */
    handleSmsVisible(mobile){
      this.smsContent.mobile = mobile
      this.smsVisible = true
    },

    /**
     * 发送短信
     */
    handleSmsSend(){
      this.infoLoading = true
      smsSend({
        mobile: this.smsContent.mobile,
        content: this.smsContent.content
      }).then(res => {
        this.infoLoading = false
        if (res.status === 'success') {
          this.$message({
            message: '短信发送成功',
            type: 'success'
          })
        }else{
          this.$message({
            message: res.failedMsg,
            type: 'error'
          })
        }
        this.smsVisible = false
      })
    },

    handleWechatVisible(mobile){
      this.wechatContent.mobile = mobile
      this.wechatVisible = true
    },

    handleWechat(){
      this.infoLoading = true
      messageToWechat({
        mobile: this.wechatContent.mobile,
        content: this.wechatContent.content
      }).then(res => {
        this.infoLoading = false
        if (res.status === 'success') {
          this.$message({
            message: '微信发送成功',
            type: 'success'
          })
        }else{
          this.$message({
            message: res.failedMsg,
            type: 'error'
          })
        }
        this.wechatVisible= false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 100px;
  display: inline-block;
}
.w200 {
  width: 200px;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
